<?php
class settingModel extends model
{
   public function setunit($model){

       $personnel = $this->post->personnel;
       if($this->post->checkuser1){
           $checkuser1 = $this->post->checkuser1;
       }else{
           $checkuser1 =array();
       }

       $data = array_merge($personnel,$checkuser1);
       $data = array_unique($data);
       $data = implode($data,',');

       $user = $_SESSION['user'];

       if($this->post->checkdet1){
           $checkdet1 = $this->post->checkdet1;
       }else{
           $checkdet1 = array();
       }

       $checkdet = array_merge($checkdet1);
       $checkdet = array_unique($checkdet);
       $checkdet = implode($checkdet,',');

       $this->dao->update(TABLE_SETUNIT)
           ->set('unit_id')->eq($checkdet)
           ->set('personnel')->eq($data)
           ->set('byuser')->eq($user->id_card)
           ->where('model')->eq($model)
           ->exec();

       return !dao::isError();
   }
    public function setlink(){
        $this->dao->delete('*')->from(TABLE_SETLINK)->exec();
        if(!$this->post->linkname){
            return true;
        }
        foreach($this->post->linkname as $key=>$val){
            if(empty($val)) continue;
             if(!empty($this->post->sort[$key])&&!is_numeric($this->post->sort[$key])) continue;

            $link = new stdclass();
            $link->sort = $this->post->sort[$key];
            $link->linkname = $this->post->linkname[$key];
            $link->linkurl  = $this->post->linkurl[$key];

            $links[] = $link;
        }
        foreach($links as $link){
            $this->dao->insert(TABLE_SETLINK)
                ->set('sort')->eq($link->sort)
                ->set('linkname')->eq($link->linkname)
                ->set('linkurl')->eq($link->linkurl)
                ->exec();
            if(!dao::isError()) $IDList[] = '1';
        }
                return $IDList;

    }


    public function setbanner(){
        $allow_type = array('jpg','jpeg','gif','png'); //定义允许上传的类型
        if($_FILES['img']){
            $file = $this->file($_FILES['img'],$allow_type);
            $this->dao->update(TABLE_SETIMG)
                ->set('imgsrc')->eq($file)
                ->set('title')->eq($this->post->title)
                ->where('model')->eq('img')
                ->exec();
        }elseif(!$_FILES['img']){
            $this->dao->update(TABLE_SETIMG)
                ->set('title')->eq($this->post->title)
                ->where('model')->eq('img')
                ->exec();
        }


        return !dao::isError();
    }
    public function setvideo(){
        $img_type =  array('jpg','jpeg','gif','png');
        $video_type =  array('wmv','rmvb','3gp','mp4','avi','flv','mpeg');
        if($_FILES['img']&&$_FILES['video']){
            $img = $this->file($_FILES['img'],$img_type);
            $video = $this->file($_FILES['video'],$video_type);
            $this->dao->update(TABLE_SETVIDEO)
                ->set('imgsrc')->eq($img)
                ->set('video')->eq($video)
                ->set('title')->eq($this->post->title)
                ->where('model')->eq('video')
                ->exec();
        }elseif($_FILES['img']&&!$_FILES['video']){
            $img = $this->file($_FILES['img'],$img_type);
            $this->dao->update(TABLE_SETVIDEO)
                ->set('imgsrc')->eq($img)
                ->set('title')->eq($this->post->title)
                ->where('model')->eq('video')
                ->exec();
        }elseif(!$_FILES['img']&&$_FILES['video']){
            $video = $this->file($_FILES['video'],$video_type);
            $this->dao->update(TABLE_SETVIDEO)
                ->set('video')->eq($video)
                ->set('title')->eq($this->post->title)
                ->where('model')->eq('video')
                ->exec();
        }

        return !dao::isError();
    }
    public function file($file,$allow_type){
        $name = $file['name'];//得到文件名称
        $type = strtolower(substr($name,strrpos($name,'.')+1)); //得到文件类型，并且都转化成小写
        //判断文件类型是否被允许上传
        if(!in_array($type, $allow_type)){
            //如果不被允许，则直接停止程序运行
            return ;
        }
        //判断是否是通过HTTP POST上传的
        if(!is_uploaded_file($file['tmp_name'])){
            //如果不是通过HTTP POST上传的
            return ;
        }
        $ym = date('Ym',time());
        $upload_path = $this->app->getAppRoot() ."www/data/upload/".$ym."/"; //上传文件的存放路径
        //开始移动文件到相应的文件夹
        if(!file_exists($upload_path)){
            mkdir($upload_path,0777,true);//创建目录
        }
            $file['name'] = time().'.'.$type;

        if(move_uploaded_file($file['tmp_name'],$upload_path.$file['name'])){
            return "www/data/upload/".$ym."/".$file['name'];
        }else{
            return false;
        }
    }

    public function addbigcategory(){

        $bigcategory = $this->post->bigcategory;
        $repetition = $this->checkbigcategory($bigcategory);
        if($bigcategory ==""){
            return $this->lang->setting->being ;
        }
        if($repetition){
            return $this->lang->setting->emptys ;
        }

            $this->dao->insert(TABLE_BIGCATEGORY)
                ->set('mistake')->eq($bigcategory)
                ->exec();
        if($this->dao->lastInsertID()){
            return $this->lang->saveSuccess;
        }else{
            return $this->lang->sqlerro;
        }


    }

    public function addproject(){

        $bid = $this->post->bigcategory;
        $project = $this->post->project;

        if($bid==""){
            return $this->lang->setting->sbigcategory;
        }

        if($project==""){
            return $this->lang->setting->emptyproject;
        }

        $repetition = $this->checkproject($bid,$project);
        if($repetition){
            return $this->lang->setting->beproject ;
        }

        $this->dao->insert(TABLE_CPROJECTS)
            ->set('mistake')->eq($project)
            ->set('bid')->eq($bid)
            ->exec();
        $id = $this->dao->lastInsertID();
        if($id){
            return $this->lang->saveSuccess;
        }else{
            return $this->lang->sqlerro;
        }
    }

    public function addcategory(){

        $pid = $this->post->bigcategory;
        $cpid = $this->post->project;
        $category = $this->post->category;
        $score = $this->post->score;
        $notification = $this->post->notification;
        $zlth = $this->post->zlth;
        $remind = $this->post->remind;
        $admonishing = $this->post->admonishing;

        if($pid==""){
            return $this->lang->setting->sbigcategory;
        }

        if($cpid==""){
            return $this->lang->setting->emptyproject;
        }

        if($category==""){
            return $this->lang->setting->emptys;
        }

        if($score == "" ){
            return $this->lang->setting->emptys;
        }

        if(!is_numeric($score)||$score<0){
            return $this->lang->setting->mastnumber;
        }

        if($notification == "" ){
            return $this->lang->setting->emptys;
        }

        if(!is_numeric($notification)||$notification<0){
            return $this->lang->setting->mastnumber;
        }


        if($remind == "" ){
            return $this->lang->setting->emptys;
        }

        if(!is_numeric($remind)||$remind<0){
            return $this->lang->setting->mastnumber;
        }

        if($zlth == "" ){
            return $this->lang->setting->emptys;
        }

        if(!is_numeric($zlth)||$zlth<0){
            return $this->lang->setting->mastnumber;
        }

        if($admonishing == "" ){
            return $this->lang->setting->emptys;
        }

        if(!is_numeric($admonishing)||$admonishing<0){
            return $this->lang->setting->mastnumber;
        }


        $repetition = $this->checkcategory($pid,$cpid,$category);

        if($repetition){
            return $this->lang->setting->being ;
        }

        $this->dao->insert(TABLE_CATEGORY)
            ->set('mistake')->eq($category)
            ->set('score')->eq($score)
            ->set('notification')->eq($notification)
            ->set('zlth')->eq($zlth)
            ->set('remind')->eq($remind)
            ->set('admonishing')->eq($admonishing)
            ->set('pid')->eq($pid)
            ->set('cpid')->eq($cpid)
            ->exec();

        $id = $this->dao->lastInsertID();
        if($id){
            return $this->lang->saveSuccess;
        }else{
            return $this->lang->sqlerro;
        }

    }

    public function editcategory($id){

        $category = $this->post->category;
        $score = $this->post->score;
        $notification = $this->post->notification;
        $zlth = $this->post->zlth;
        $remind = $this->post->remind;
        $admonishing = $this->post->admonishing;

        if($category==""){
            return $this->lang->setting->emptys;
        }

        if($score == "" ){
            return $this->lang->setting->emptys;
        }
        if(!is_numeric($score)||$score<0){
            return $this->lang->setting->mastnumber;
        }

        if($notification == "" ){
            return $this->lang->setting->emptys;
        }

        if(!is_numeric($notification)||$notification<0){
            return $this->lang->setting->mastnumber;
        }


        if($remind == "" ){
            return $this->lang->setting->emptys;
        }

        if(!is_numeric($remind)||$remind<0){
            return $this->lang->setting->mastnumber;
        }

        if($zlth == "" ){
            return $this->lang->setting->emptys;
        }

        if(!is_numeric($zlth)||$zlth<0){
            return $this->lang->setting->mastnumber;
        }

        if($admonishing == "" ){
            return $this->lang->setting->emptys;
        }

        if(!is_numeric($admonishing)||$admonishing<0){
            return $this->lang->setting->mastnumber;
        }

        $this->dao->update(TABLE_CATEGORY)
            ->set('mistake')->eq($category)
            ->set('score')->eq($score)
            ->set('notification')->eq($notification)
            ->set('zlth')->eq($zlth)
            ->set('remind')->eq($remind)
            ->set('admonishing')->eq($admonishing)
            ->where('id')->eq($id)
            ->exec();
        if(!dao::isError()){
            return $this->lang->editSuccess;
        }else{
            return $this->lang->sqlerro;
        }
    }

    public function editproject($id){

        $project = $this->post->project;

        if($project==""){
            return $this->lang->setting->emptys;
        }
        $this->dao->update(TABLE_CPROJECTS)
            ->set('mistake')->eq($project)
            ->where('id')->eq($id)
            ->exec();
        if(!dao::isError()){
            return $this->lang->editSuccess;
        }else{
            return $this->lang->sqlerro;
        }
    }

    public function editbigcategory($id){

        $bigcategory = $this->post->bigcategory;

        if($bigcategory==""){
            return $this->lang->setting->emptys;
        }
        $this->dao->update(TABLE_BIGCATEGORY)
            ->set('mistake')->eq($bigcategory)
            ->where('id')->eq($id)
            ->exec();
        if(!dao::isError()){
            return $this->lang->editSuccess;
        }else{
            return $this->lang->sqlerro;
        }
    }


    public function addcategorys($pid,$cpid){

        foreach($this->post->category as $key=>$value){
            if($value=="") continue;
            if($this->post->score[$key] == "" || !is_numeric($this->post->score[$key])||$this->post->score[$key]<0){
                continue;
            }
            $res = $this->checkcategory($pid,$cpid,$value);
            if($res) continue;

            $this->dao->insert(TABLE_CATEGORY)
                ->set('mistake')->eq($value)
                ->set('score')->eq($this->post->score[$key])
                ->set('notification')->eq($this->post->notification[$key])
                ->set('zlth')->eq($this->post->zlth[$key])
                ->set('remind')->eq($this->post->remind[$key])
                ->set('admonishing')->eq($this->post->admonishing[$key])
                ->set('pid')->eq($pid)
                ->set('cpid')->eq($cpid)
                ->exec();
            if(!dao::isError()) $IDList[] = $this->dao->lastInsertID();
        }

        if($IDList){
           return $this->lang->saveSuccess;
        }else{
            return $this->lang->sqlerro;
        }

    }

    public function delcategory($id){

        $this->dao->delete()->from(TABLE_CATEGORY)->where('id')->eq($id)->exec();
        if(!dao::isError()){
            $data = array(
                'erro'=>'1',
                'data'=>'删除成功',
            );
        }else{
            $data = array(
                'erro'=>'0',
                'data'=>'删除失败',
            );
        }
        return json_encode($data);
    }

    public function delproject($id){

        $this->dao->delete()->from(TABLE_CPROJECTS)->where('id')->eq($id)->exec();
        $this->dao->delete()->from(TABLE_CATEGORY)->where('cpid')->eq($id)->exec();
        if(!dao::isError()){
            $data = array(
                'erro'=>'1',
                'data'=>'删除成功',
            );
        }else{
            $data = array(
                'erro'=>'0',
                'data'=>'删除失败',
            );
        }
        return json_encode($data);
    }

    public function delbigcategory($id){

        $this->dao->delete()->from(TABLE_BIGCATEGORY)->where('id')->eq($id)->exec();
        $this->dao->delete()->from(TABLE_CPROJECTS)->where('bid')->eq($id)->exec();
        $this->dao->delete()->from(TABLE_CATEGORY)->where('pid')->eq($id)->exec();
        if(!dao::isError()){
            $data = array(
                'erro'=>'1',
                'data'=>'删除成功',
            );
        }else{
            $data = array(
                'erro'=>'0',
                'data'=>'删除失败',
            );
        }
        return json_encode($data);
    }


    public function checkbigcategory($bigcategory){
        $repetition = $this->dao->select('id')->from(TABLE_BIGCATEGORY)
                            ->where('mistake')->eq($bigcategory)
                            ->fetch();
        if($repetition){
            return true;
        }else{
            return false;
        }
    }

    public function checkproject($bid,$project){

        $repetition = $this->dao->select('id')->from(TABLE_CPROJECTS)
                            ->where('mistake')->eq($project)
                            ->andWhere('bid')->eq($bid)
                            ->fetch();
        if($repetition){
            return true;
        }else{
            return false;
        }

    }

    public function checkcategory($pid,$cpid,$mistake){

        $repetition = $this->dao->select('id')->from(TABLE_CATEGORY)
            ->where('mistake')->eq($mistake)
            ->andWhere('pid')->eq($pid)
            ->andWhere('cpid')->eq($cpid)
            ->fetch();
        if($repetition){
            return true;
        }else{
            return false;
        }

    }

    public function getproject($bid){
        $types   = $this->dao->select('id, mistake')->from(TABLE_CPROJECTS)->where('bid')->eq($bid)->fetchPairs();
        foreach($types as $id => $mistake) if($mistake == '') $types[$id] = $mistake;
        return array('' => '') + $types;
    }

    public function getunit($model){
        $departments = $this->dao->select('unit_id,personnel')->from(TABLE_SETUNIT)->where('model')->eq($model)->fetch();
       return $departments;
    }

    public function getlink(){
        $links = $this->dao->select('*')->from(TABLE_SETLINK)->orderBy('sort asc')->fetchAll();
        return $links;
    }
    public function getbanner(){
        $banner = $this->dao->select('*')->from(TABLE_SETIMG)->where('model')->eq('img')->fetch();
        return $banner;
    }

    public function getvideo(){
        $video = $this->dao->select('*')->from(TABLE_SETVIDEO)->where('model')->eq('video')->fetch();
        return $video;
    }

    public function getcatecoryall(){
        $bigcategory = $this->loadModel('statistics')->getbigcatecory();
        foreach($bigcategory as $key=>$bigval){
            $cprojects = $this->dao->select('count(*) as count')->from(TABLE_CPROJECTS)->where('bid')->eq($bigval->id)->orderBy('id asc')->fetch();
            $bigval->count = $cprojects->count;
            $pro = $this->getbybid($bigval->id);
            $bigval->project = $pro;
            foreach($bigval->project as $value){
                $count = $this->dao->select('count(*) as count')->from(TABLE_CATEGORY)->where('cpid')->eq($value->id)->orderBy('id asc')->fetch();
                $value->count = $count->count;
                if($value->count>1){
                    $bigval->count = $bigval->count+$value->count-1;
                }
                $category = $this->dao->select('*')->from(TABLE_CATEGORY)->where('cpid')->eq($value->id)->orderBy('id asc')->fetchAll();
                $value->category = $category;
            }
        }

        return $bigcategory;
    }


    public function getbybid($bid){
        $project = $this->dao->select('*')->from(TABLE_CPROJECTS)->where('bid')->eq($bid)->fetchAll();
        return $project;
    }

    public function getcategorybyid($id){
        $category = $this->dao->select('*')->from(TABLE_CATEGORY)
                        ->where('id')->eq($id)
                        ->fetch();
        return $category;
    }

    public function getprojectbyid($id){
        $project = $this->dao->select('*')->from(TABLE_CPROJECTS)->where('id')->eq($id)->fetch();
        return $project;
    }

    public function getbigcategorybyid($id){

        $bigcategory = $this->dao->select('*')->from(TABLE_BIGCATEGORY)->where('id')->eq($id)->fetch();
        return $bigcategory;
    }



}